package com.wn.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wn.bean.PageData;
import com.wn.bean.Result;
import com.wn.pojo.Trainer;
import com.wn.service.ITrainerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 培训师 前端控制器
 * </p>
 *
 * @author yellow docker
 * @since 2023-01-25
 */
@Api(tags = "trainer模块管理")
@RestController
@RequestMapping("/trainer")
public class TrainerController extends BaseController {

    @Autowired
    ITrainerService trainerService;

    @ApiOperation("根据Id查询培训师信息")
    @GetMapping("{id}")
    public Result getbyid(@PathVariable String id) {
        Trainer trainer = trainerService.getById(id);
        return toDataResult(trainer);

    }

    @ApiOperation("分页查询操作")
    @GetMapping("page/{currentPage}/{pageSize}")
    public Result searchPageInfomation(@PathVariable Integer currentPage, @PathVariable Integer pageSize) {
        Page<Trainer> p = new Page<>(currentPage, pageSize);
        Page<Trainer> pagelist = trainerService.page(p);
        long total = pagelist.getTotal();
        List<Trainer> records = pagelist.getRecords();

        PageData pageData = new PageData(total, records);
        return  toDataResult(pageData);
    }


    @ApiOperation("新增培训师信息")
    @PostMapping
    public Result saveTrainer(@RequestBody Trainer trainer) {
        boolean save = trainerService.save(trainer);
        
        return save?success():error();

    }
    @ApiOperation("根据id删除培训师信息")
    @DeleteMapping("{id}")
    public Result deleteTrainer(@PathVariable Integer id){
        boolean delete = trainerService.removeById(id);
        return delete?success():error();
    }

    @ApiOperation("根据id修改培训师信息")
    @PutMapping
    public Result updateTrainer(@RequestBody Trainer trainer){
        boolean update = trainerService.updateById(trainer);


        return update?success():error();

    }



}
